﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TNAV04
    {
        #region ==PC===========
        public static readonly DependencyProperty PCProperty = DependencyProperty.Register("PC",
        typeof(string), typeof(TNAV04),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPCChanged));
        public string PC
        {
            get
            {
                return (string)GetValue(PCProperty);
            }
            set
            {
                SetValue(PCProperty, value);
            }
        }
        private static void OnPCChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TNAV04;
            if (src != null)
            {
                src.PC = (string)e.NewValue;
            }
        }
        #endregion
         
        public TNAV04()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABPOS();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            txtQ.IsEnabled = false;

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwAB = dwMaster;
            if (dwMaster == null)
            {
                QTY = 0;

            }
            else
            {
                QTY = System.Convert.ToDouble(dwMaster["ABPTP"].ToString());
            }
            //此时规则和AB52应当进行了装载
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 11)
                return;
            dwData = TBMain.NewRow();
            dwData["ABPMANDANT"] = app.Mandant;
            dwData["ABPAUFKRS"] = EXAUF.Substring(0, 2);
            dwData["ABPAUFLFD"] = EXAUF.Substring(2, 6);
            dwData["ABPAUFPOS"] = EXAUF.Substring(8, 3);
            SResult = TMV.QABP44(dwData);
            MergeData(SResult, true);
            if(TBMain.Rows.Count>1)
            {
                MessageBox.Show(dwMaster["QPZPNRKRS"].ToString()+dwMaster["QPZPLFDNR"].ToString()+ "质检计划有误，请核对");
                TBMain.Rows.RemoveAt(0);
            }
            base.OnEXAUFChg(vlaue);
        }
        public int dy = 0;

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {

                if (!IsNew)
                {
                    IsNew = true;
                    dy = System.Convert.ToInt16(QTY);
                    QTY = 0;
                    txtQ.IsEnabled = true;
                    return;
                }
                if (QTY <= 0)
                    return;
                //TBMain.Clear();
                int abptp = 0;
                string ARTICLE = "", WID = "", FIN = "", TRA = "", ART = "", DES = "";
                ARTICLE = dwAB["ARTICLE"].ToString();
                WID = dwAB["WID"].ToString();
                FIN = dwAB["FIN"].ToString();
                TRA = dwAB["TRA"].ToString();
                ART = dwAB["ART"].ToString();
                DES = dwAB["DES"].ToString();
                for (int tp = 1; tp <= QTY; tp++)
                {
                    abptp = dy + tp;
                    if (tp >= 2)
                        dwData = TBMain.Rows.Add();
                    else
                    {
                        dwData = TBMain.Rows[0];
                    }
                    dwData["WBBTP"] = EXAUF + Helper.Right("0000" + abptp.ToString(), 4);
                    dwData["ARTICLE"] = ARTICLE;
                    dwData["WID"] = WID;
                    dwData["FIN"] = FIN;
                    dwData["TRA"] = TRA;
                    dwData["ART"] = ART;
                    dwData["DES"] = DES;

                }
                IsNew = false;
                QTY = dy + QTY;
                dwAB["ABPMANDANT"] = app.Mandant;
                dwAB["ABPTP"] = QTY.ToString();
                TMV.UABP17(dwAB);
                base.NewCommand(sender, e);
                txtQ.IsEnabled = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void PrintCommand(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            Set.Merge(TBMain);
            cDR.PRINT(Set);
        }

        public void PrintAFrameCard(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            DataSet SetResult = new DataSet();
            dwData["ABPFILLER1"] = EXAUF; 
            dwMaster["WBBTP"] = EXAUF + PC;
            PM["KRS"] = EXAUF.Substring(0, 2);
            PM["LFD"] = EXAUF.Substring(2, 6);
            PM["POS"] = EXAUF.Substring(8, 3);
            SResult = TMV.VABF.LOADABF(PM);
            SetResult = TMV.TXT.QTXT06(dwData);
            Set.Merge(SetResult);
            Set.Merge(SResult);  
            Set.Merge(TBMain);
            cDR2.PRINT(Set);
        }

        public void PrintCom(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            int tq = 0;
            tq = int.Parse(PC);
            if (tq <= 0) return;
            if (tq > QTY)
            {
                MessageBox.Show("补打批号必须是已产生的批号！");
                return;
            }
            string ARTICLE = "", WID = "", FIN = "", TRA = "", ART = "", DES = "", ABPAUFKRS = "", ABPAUFLFD = "", ABPAUFPOS = "";
            ABPAUFKRS = dwAB["ABPAUFKRS"].ToString();
            ABPAUFLFD = dwAB["ABPAUFLFD"].ToString();
            ABPAUFPOS = dwAB["ABPAUFPOS"].ToString();
            ARTICLE = dwAB["ARTICLE"].ToString();
            WID = dwAB["WID"].ToString();
            FIN = dwAB["FIN"].ToString();
            TRA = dwAB["TRA"].ToString();
            ART = dwAB["ART"].ToString();
            DES = dwAB["DES"].ToString();
            TBMain.Clear();
            dwData = TBMain.Rows.Add();
            dwData["WBBTP"] = EXAUF + PC;
            dwData["ABPAUFKRS"] = ABPAUFKRS;
            dwData["ABPAUFLFD"] = ABPAUFLFD;
            dwData["ABPAUFPOS"] = ABPAUFPOS;
            dwData["ARTICLE"] = ARTICLE;
            dwData["WID"] = WID;
            dwData["FIN"] = FIN;
            dwData["TRA"] = TRA;
            dwData["ART"] = ART;
            dwData["DES"] = DES;
            Set.Merge(TBMain);
            cDR1.PRINT(Set);
            OnEXAUFChg(EXAUF);
        }
    }
}